﻿/****************************** Module Header ******************************\
 * Module Name:  mxControlCollection.cs
 * Module Version:  1.0.0 (2012-9-5)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension winformcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

namespace UCL.Forms
{
    public static class mxControlCollection
    {
        /// <summary>
        /// 锁定控件
        /// <para>2012-11-20 解决combobox背景颜色问题</para>
        /// </summary>
        /// <param name="cc"></param>
        /// <param name="enables"></param>
        public static void Lock(this IList<Control> cc, Control[] enables = null)
        {
            bool hasControl = false;
            foreach (Control c in cc)
            {
                if (enables != null)
                {
                    hasControl = false;
                    foreach (Control c1 in enables)
                    {
                        if (c1 == c) { hasControl = true; break; }
                    }
                }
                if (hasControl || c.GetType() == typeof(Button) || c.GetType() == typeof(Label)) continue;
                if (c.GetType() == typeof(TextBox))
                {
                    ((TextBox)c).Lock();
                    continue;
                }
                if (c.GetType() == typeof(ComboBox))
                {
                    ((ComboBox)c).Lock();
                    continue;
                }
                c.Enabled = false;
            }
        }

        /// <summary>
        /// 获取选中的RadioButton的属性值
        /// <para>2013-9-6 by sutroon</para>
        /// <para>如：new RadioButton[] { rbTypeDisplay, rbTypeMenu, rbTypeOperate }.GetCheckedValue&lt;string&gt;(a =&gt; a.Text, "视图")</para>
        /// <para>本方法用于取代GetRadioButtonGroupValue方法</para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lst"></param>
        /// <param name="func"></param>
        /// <param name="defv"></param>
        /// <returns></returns>
        public static T GetCheckedValue<T>(this IList<RadioButton> lst, Func<RadioButton, T> func, T defv)
        {
            foreach (RadioButton rb in lst)
            {
                if (rb.Checked) return func(rb);
            }
            return defv;
        }

        /// <summary>
        /// 设置RadioButton群组的选中状态
        /// <para>2013-9-6 by sutroon</para>
        /// <para>如：new RadioButton[] { rbTypeDisplay, rbTypeMenu, rbTypeOperate }.SetCheckedValue(a =&gt; { return a.Text == dt.GetString("Interaction"); });</para>
        /// <para>本方法用于取代SetRadioButtonGroupValue方法</para>
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="func"></param>
        public static void SetCheckedValue(this IList<RadioButton> lst, Func<RadioButton, bool> func)
        {
            foreach (RadioButton rb in lst)
            {
                if (func(rb)) rb.Checked = true;
            }
        }
    }
}
